package cn.com.twoke.game.client.games.gomoku.controller;

import cn.com.twoke.game.client.GameClientManager;
import cn.com.twoke.game.client.games.gomoku.GomokuGame;
import cn.com.twoke.game.client.games.gomoku.entity.data.DropData;
import cn.com.twoke.game.client.games.gomoku.entity.data.UndoData;
import cn.com.twoke.game.common.entity.message.GameChatMessage;

import javax.swing.*;

public class NioController extends AbstractGomokuController{

    @Override
    public void drop(int x, int y) {
        GameClientManager.INSTANCE.sendGameData(
                new DropData(x,y, this.game.getUserDropingChessType(),
                this.game.getPlayerName()), GomokuGame.ID, this.game.getRoomId());
    }

    @Override
    public void undo() {
        if (this.game.getChessNotes().get(this.game.getChessNotes().size() - 1).getType() != this.game.getUserDropingChessType()) {
            JOptionPane.showMessageDialog(this.game.getWindow(), "没有可悔棋了");
            return;
        }

        GameClientManager.INSTANCE.sendGameData(
                new UndoData(this.game.getUserDropingChessType(),
                        this.game.getPlayerName()), GomokuGame.ID, this.game.getRoomId());
    }

    @Override
    public void gameOver() {
        // 退出系统
        int i = JOptionPane.showConfirmDialog(this.game.getWindow(),
                "确认要结束游戏？"

        );
        if (i == JOptionPane.OK_OPTION) {
            GameClientManager.INSTANCE.stopGame(GomokuGame.ID, this.game.getRoomId(), this.game.getPlayerName());
        }
    }

    @Override
    public void sendMessage(String content) {
        GameClientManager.INSTANCE.sendMessage(new GameChatMessage(content,
                this.game.getPlayerName(),
                this.game.getRoomId(),
                GomokuGame.ID,
                System.currentTimeMillis()));
    }


}
